<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Comment {

	public function start() {
		global $cache,$request;

		$action = $request->get( 'p' );

		switch( $action ) {
		    case 'batch':
		    	$this->batch();
		    	break;
		    default:
		    	$this->show( $action );
		}
	}

	private function batch() {
		global $db,$cache,$request;
		$id_arr = array();
		$id_arr = $request->get( 'id_arr','P','array' );
		$control = $request->get( 'Control','P' );
		$size = count( $id_arr );
		if( $size == 0 ) {
			echo '<p class="error">错误：您没有选中任何评论！</p>';
			return;
		}
		$sql = '';
		for( $i=0; $i<$size; $i ++ ){
			$sql .= 'cid=' . $id_arr[$i] . ' OR ';
		}
		$sql = substr( $sql,0,-4 );

		if( $control == '删除' ) {
			// 处理日志评论数
			$temp_arr = $db->fetch_all( "SELECT articleid FROM `" . DB_PREFIX . "comment` WHERE ( $sql )" );
			$size = count( $temp_arr );
			for( $i=0; $i<$size; $i ++ ){
				$db->query( 'UPDATE `' . DB_PREFIX . 'article` SET comment=comment-1 WHERE id=' . $temp_arr[$i]['articleid'] );
			}
			// 删除评论
			$db->query( 'UPDATE `' . DB_PREFIX . 'comment` SET isdel=1 WHERE (' . $sql . ')' );
			// 刷新缓存
			$cache->refresh( 'comment_new', TRUE );
			$cache->refresh( 'guestbook_new', TRUE );
			$cache->refresh( 'article_hot', TRUE );
			$cache->refresh( 'count', TRUE );
			echo '<p class="warning">批量删除评论成功</p>';
		}elseif( $control == '通过' ) {
			$sql = 'UPDATE `' . DB_PREFIX . 'comment` SET isshow=1 WHERE (' . $sql . ')';
			$db->query( $sql );
			// 刷新缓存
			$cache->refresh( 'comment_new', TRUE );
			$cache->refresh( 'guestbook_new', TRUE );
			$cache->refresh( 'article_hot', TRUE );
			$cache->refresh( 'count', TRUE );
			echo '<p class="warning">批量审核评论成功</p>';
		}else{
			echo '<p class="error">错误：未定义操作！</p>';
		}
	}

	private function show( $action, $message = '', $type = '' ) {
		global $cache;
		if( $message != '' ) {
			$message = '<p class="' . $type . '">' . $message . '</p>';
		}
		@header("content-type: text/html; charset=utf-8");
?>
<div class="admin_panel">
	<?php
	switch( $action ) {
    case 'list':
        $this->comment_list();
    	break;
    case 'censor':
        $this->comment_list( 'censor' );
    	break;
	default:
		$this->main();
	}
	?>
	<script>
		$(document).ready(function(){
			$('#ajax_form').ajaxForm(function(data){
				showMessage(data);
				$("#admin_loading").css('visibility','hidden');
			});
		});
	</script>
</div>
<?php
	}

	private function comment_list( $action = 'list' ) {
		global $db,$cache,$request;
		$current_page = max(1, $request->get( 'page' ));
		$max_per_page = 20;
		$offset = ($current_page - 1) * $max_per_page;
		if( $action == 'list' ) {
			$sql = 'SELECT cid,author,time,comment,reply,replytime FROM `' . DB_PREFIX . "comment` WHERE isdel=0 AND isshow<>0 ORDER BY cid DESC LIMIT $offset, $max_per_page";
		}else{
			$sql = 'SELECT cid,author,time,comment,reply,replytime FROM `' . DB_PREFIX . "comment` WHERE isdel=0 AND isshow=0 ORDER BY cid DESC LIMIT $offset, $max_per_page";
		}
		$comment_array = $db->fetch_all( $sql );
?>
<div class="admin_title"><?php if( $action == 'list' ) echo L('_COMMENT_LIST_'); else echo L('_COMMENT_CENSOR_'); ?></div>
<div class="admin_content">
	<form name="smslist" action="index.php?m=comment&p=batch" method="post" id="ajax_form">
		<div style="padding:5px;margin:2px;">      
			<table class="pickme" style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
				<thead>
					<tr align="center" height="22" bgcolor="#ffffff">
						<td width="3%"><input type="checkbox" name="allbox" onclick="CheckAll();"><input type="checkbox" id="ckbox" style="display:none;" disabled></td>
						<td width="4%">序号</td>
						<td width="10%">作者</td>
						<td width="31%">内容</td>
						<td width="11%">时间</td>
						<td width="21%">回复</td>
						<td width="11%">回复时间</td>
						<td width="9%">管理操作</td>
					</tr>
				</thead>
				<tbody>
					<?php
					$i = 1;
					foreach( $comment_array as $value ) {
					?>
					<tr align="center" height="20" bgcolor="#ffffff">
						<td ><input type="checkbox" id="ckbox" name="id_arr[]" value="<?php echo $value['cid'] ?>" /></td>
						<td><?php echo ( $current_page - 1 ) * $max_per_page + $i ?></td>
						<td><?php echo $value['author'] ?></td>
						<td style="text-align:left;padding-left:3px;"><?php echo cut_str( $value['comment'],30 ) ?></td>
						<td><?php echo date( 'Y-m-d', $value['time'] ) ?></td>
						<td style="text-align:left;padding-left:3px;"><?php echo cut_str( $value['reply'],20 ) ?></td>
						<td><?php if( $value['reply'] != '' ) echo date( 'Y-m-d', $value['replytime'] ); ?></td>
						<td>
							<span class="Control">
							<a href="#" onclick="return false;">回复</a>
							</span>
						</td>
					</tr>
					<?php
						$i ++;
					}
					?>
				</tbody>
			</table>
		</div>
		<?php if( $action == 'list' ) {
			$comment_num = $cache->count['comment_num'] + $cache->count['guestbook_num'];
			echo Admin::get_navigator( $comment_num, $max_per_page, $current_page, 'comment', 'list' ); ?>
			<div align="center" style="margin-top:10px;">批量管理选项: <input type="radio" name="Control" value="删除" checked="checked"/>删除 <input type="submit" onclick="$('#admin_loading').css('visibility','visible');" class="main_button" value="执行操作" /></div>
		<?php }else{
			$comment_num = $cache->count['comment_num_censor'] + $cache->count['guestbook_num_censor'];
			echo Admin::get_navigator( $comment_num, $max_per_page, $current_page, 'comment', 'censor' ); ?>
			<div align="center">批量管理选项: <input type="radio" name="Control" value="通过" checked="checked"/>通过 <input type="radio" name="Control" value="删除"/>删除 <input type="submit" onclick="$('#admin_loading').css('visibility','visible');" class="main_button" value="执行操作" /></div>
		<?php } ?>
	</form>
	<div class="main_button"></div>
</div>
<?php
	}

	private function main() {
?>
<div class="admin_title"><?php echo L('_CONTENT_MANAGEMENT_'); ?></div>
<div class="admin_content">
	<div style="padding:10px;">
		<?php echo L('_UNDEFINED_ACTION_'); ?>
	</div>
</div>
<?php
	}
}
?>
